package com.hot12345.servlet;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hot12345.common.util.MapUtil;
import com.hot12345.common.util.ReadProperties;
import com.hot12345.server.DBConnection.IbatisConnectCommImpl;
import com.hot12345.job.JobTrigger;

public class InitServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2836921232351346284L;

	private static Log logger = LogFactory.getLog(InitServlet.class);

	 public InitServlet() {
	  super();
	 }

	 public void destroy() {
	  super.destroy(); 
	 }

	 public void init() throws ServletException {
	  logger.info("开始初始化连接池");
	  initDataSource();
	  logger.info("初始化连接池结束");
	  
	  initCacheSite();
	  
	  startInvalidOrderJob();
	 }

	 
	 /**
	  * 初始化数据库连接池设置，并创建好连接。类似于通过在web.xml配置初始化Servlet方式一样。
	  * 
	  * @author wuzongbao
	  * @date 2012-6-9 下午05:00:41
	  */
	 private void initDataSource(){
		 
		Map<String,Object> propertisMap = ReadProperties.readValueAll("/dataSourceAlias.properties");
		if(propertisMap==null){
			logger.error("初始化数据库连接池错误,propertisMap==null");
			return;
		}
		Iterator iter = propertisMap.entrySet().iterator();
		try{
			while (iter.hasNext()) {
			    Map.Entry entry = (Map.Entry) iter.next();
			    if(entry==null){
			    	logger.error("初始化数据库连接池错误,entry==null");continue;
			    }
			    IbatisConnectCommImpl.getSqlMapClient(entry.getKey().toString());
			}
		}catch(Exception e){
			logger.error("初始化数据库连接池错误",e);
			MapUtil.printMap(propertisMap);
		}
		 
	 }
	 
	 private void startInvalidOrderJob(){
		JobTrigger.init();
		try {
			JobTrigger.appendJobByClassPath("CreateIndexJob", "0 0/1 * * * ?",//每5分钟执行一次。
					"com.hot12345.job.impl.CreateIndexHtmlJob");
		} catch (Exception e) {
			logger.error("启动首页静态化定时器异常", e);
		}
	 }
	 
	 private void initCacheSite(){
		 
	 }
}
